package com.bwei.like.mynewstop.adapater;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.bwei.like.mynewstop.Bean.OffLineBean;
import com.bwei.like.mynewstop.R;
import com.bwei.like.mynewstop.base.MyBaseAdapater;

import java.util.ArrayList;

/**
 * Created by LiKe on 2016/11/16.
 */
public class OffLineListViewAdapater extends MyBaseAdapater<OffLineBean> {
    private CheckBox select_All_cb;
    private int tag = 0;

    public OffLineListViewAdapater(ArrayList arrayList, Context context, CheckBox select_All_cb) {
        super(arrayList, context);
        this.select_All_cb = select_All_cb;
    }

    @Override
    public View initView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.off_line_listview_item, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.offlineitemtv.setText(arrayList.get(position).getCategory());
        //监听cb按钮
        viewHolder.offlineitemcheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (viewHolder.offlineitemcheck.isChecked()) {
                    tag++;
                    arrayList.get(position).setChecked(true);
                } else {
                    tag--;
                    arrayList.get(position).setChecked(false);
                }

                if (tag == arrayList.size()) {
                    select_All_cb.setChecked(true);
                    select_All_cb.setText("取消全选");
                } else {
                    select_All_cb.setChecked(false);
                    select_All_cb.setText("全选");
                }
            }
        });


//
//        //CheckBox监听全选
        select_All_cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (select_All_cb.isChecked()) {
                    for (int i = 0; i < arrayList.size(); i++) {
                        arrayList.get(i).setChecked(true);
                    }
                    select_All_cb.setText("取消全选");
                    tag = arrayList.size();
                } else {
                    for (int i = 0; i < arrayList.size(); i++) {
                        arrayList.get(i).setChecked(false);
                    }
                    select_All_cb.setText("全选");
                    tag = 0;
                }
                OffLineListViewAdapater.this.notifyDataSetChanged();
            }
        });


        viewHolder.offlineitemcheck.setChecked(arrayList.get(position).isChecked());


        return convertView;
    }

    public class ViewHolder {
        public final TextView offlineitemtv;
        public final CheckBox offlineitemcheck;
        public final View root;

        public ViewHolder(View root) {
            offlineitemtv = (TextView) root.findViewById(R.id.offline_item_tv);
            offlineitemcheck = (CheckBox) root.findViewById(R.id.offline_item_check);
            this.root = root;
        }
    }
}
